/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.sumofseries.threads;

import ua.yandex.sumofseries.utilconcurrent.Range;
import ua.yandex.sumofseries.utilconcurrent.TaskManager;

/**
 *
 * @author vlad
 */
public class SinSum {
    
    private static final long MULT = 10000;
    private Range[] ranges;
    
    public SinSum(int N, int poolSize) {
        if(poolSize <= 0) {
            throw new IllegalArgumentException("There should be at"
                    + " least one thread executing task");
        }
        long intervLen = 2L * N * MULT / poolSize;
        long currPoint = - N * MULT;
        ranges = new Range[poolSize];
        for(int i = 0; i < poolSize - 1; i++) {
            ranges[i] = new Range(currPoint, currPoint + intervLen - 1);
            currPoint += intervLen;
        }
        ranges[poolSize - 1] = new Range(currPoint, N * MULT);
    }
    
    public double calcSum() {
        return TaskManager.execute(ranges);
    }
    
}
